package srdb.migration.dbhandler.mysql;


import java.io.FileInputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import srdb.migration.Util.DBInfo;
import srdb.migration.Utility;
import srdb.migration.common.MTKConnection;

public class MySQLConnection extends MTKConnection {
    private static String mysqlDriver = "com.mysql.jdbc.Driver";

    public MySQLConnection(String dbURL, String dbUser, String dbPassword)
            throws Exception {
        super(dbURL, dbUser, dbPassword);
        try {
            Class.forName(mysqlDriver);

            this.conn = DriverManager.getConnection(appendParams(dbURL), dbUser, dbPassword);
        } catch (SQLException se) {
            Exception exp = Utility.processException(se);
            throw new Exception("Following problem occurred while connecting to MySQL server.\n" + exp.getMessage(), exp);
        } catch (Exception e) {
            e = Utility.processException(e);
            throw new Exception("Following problem occurred while connecting to MySQL server.\n" + e.getMessage(), e);
        }
    }

    public MySQLConnection(String propertiesFile)
            throws Exception {
        super(propertiesFile);
        try {
            Properties ini = new Properties();
            ini.load(new FileInputStream(propertiesFile));

            this.dbURL = appendParams(ini.getProperty("mysqlConnection"));
            this.dbUser = ini.getProperty("mysqlUser");
            this.dbPassword = ini.getProperty("mysqlPassword");

            Class.forName(mysqlDriver);

            this.conn = DriverManager.getConnection(this.dbURL, this.dbUser, this.dbPassword);
        } catch (SQLException se) {
            Exception exp = Utility.processException(se);
            throw new Exception("Following problem occurred while connecting to MySQL server.\n" + exp.getMessage(), exp);
        } catch (Exception e) {
            e = Utility.processException(e);
            throw new Exception("Following problem occurred while connecting to MySQL server.\n" + e.getMessage(), e);
        }
    }

    private String appendParams(String dbURL) {
        if (!dbURL.contains("?"))
            dbURL = dbURL + "?";
        else {
            dbURL = dbURL + "&";
        }
        dbURL = dbURL + "yearIsDateType=false&connectTimeout=10000";
        return dbURL;
    }


    @Override
    public boolean preConnectDB(String[] parms) throws Exception {
        //todo
        return false;
    }
}

/* 

 * 
 */